<?php

class Mithos_Socket {
    private $_host;
    private $_port;
    private $_sock;

    public function __construct($host, $port) {
        $this->_host = $host;
        $this->_port = $port;
    }

    public function connect() {
        $this->_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if (!$connect = socket_connect($this->_sock, $this->_host, $this->_port)) {
            throw new Mithos_Socket_Exception('Unable to connect socket');
        }
        return $connect;
    }

    public function send($data) {
        if (socket_write($this->_sock, $data) == false) {
            throw new Mithos_Socket_Exception('Failed to send packet');
        }
    }

    public function read($length = 2048) {
        return socket_read($this->_sock, $length);
    }

    public function kill() {
        socket_close($this->_sock);
    }

    public function __destruct() {
        $this->kill();
    }
}

?>